package com.yjr.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yjr.gulimall.product.entity.BrandEntity;
import com.yjr.gulimall.product.entity.CategoryEntity;
import com.yjr.gulimall.product.service.BrandService;
import com.yjr.gulimall.product.service.CategoryService;
import com.yjr.gulimall.product.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjr.common.utils.PageUtils;
import com.yjr.common.utils.Query;

import com.yjr.gulimall.product.dao.CategoryBrandRelationDao;
import com.yjr.gulimall.product.entity.CategoryBrandRelationEntity;
import com.yjr.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.util.CollectionUtils;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    @Autowired
    private  BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private  CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //保存品牌分类关联的时候，需要顺便保存品牌名字和分类名字

        CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(categoryBrandRelation.getBrandId());
        categoryBrandRelationEntity.setCatelogId(categoryBrandRelation.getCatelogId());

        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        if(brandEntity!=null){
            categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        }
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        if(categoryEntity!=null){
            categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
        }
        this.save(categoryBrandRelationEntity);

    }

    @Override
    public void updateBrand(CategoryBrandRelationEntity categoryBrandRelation) {

        this.update(categoryBrandRelation,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",categoryBrandRelation.getBrandId()));
    }

    @Override
    public void updateCategory(Long catId, String name) {

        categoryBrandRelationDao.updateCategory(catId,name);
    }

    @Override
    public List<BrandVO> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<BrandVO> collect=new ArrayList<>();
        if(!CollectionUtils.isEmpty(categoryBrandRelationEntities)){

            collect = categoryBrandRelationEntities.stream().map(
                    categoryBrandRelationEntity -> {
                        BrandVO brandVO = new BrandVO();
                        brandVO.setBrandId(categoryBrandRelationEntity.getBrandId());
                        brandVO.setBrandName(categoryBrandRelationEntity.getBrandName());
                        return brandVO;
                    }
            ).collect(Collectors.toList());
        }

        return  collect;
    }

}